package handler

import (
	"awesomeProject/app/model"
	"awesomeProject/app/request"
	"awesomeProject/app/response"
	"awesomeProject/app/service"
	"awesomeProject/app/verify"
	"awesomeProject/util"
	"github.com/gin-gonic/gin"
	"strconv"
)

type AccountHandler struct{}

var (
	accountSrv service.AccountSrv
)

// Pagination 账户列表（分页）
func (r *AccountHandler) Pagination(ctx *gin.Context) {
	// 表单验证
	var req request.AccountPagination
	_ = ctx.Bind(&req)
	if err := verify.AccountPagination(&req); err != nil {
		response.Result(ctx, response.ErrInvalidParam.SetMsg(err.Error()))
		return
	}
	// 操作人
	response.Result(ctx, accountSrv.Pagination(req, util.CurrentLogin(ctx)))
	return
}

// Create 账户创建
func (r *AccountHandler) Create(ctx *gin.Context) {
	// 表单校验
	var req request.AccountCreate
	_ = ctx.Bind(&req)
	if err := verify.AccountCreate(&req); err != nil {
		response.Result(ctx, response.ErrInvalidParam.SetMsg(err.Error()))
		return
	}
	// 校验原密码与加密密码是否匹配
	hashPwd, err := accountSrv.GetHashPwd(req.OriginPwd)
	if err != nil {
		response.Result(ctx, response.ErrQuery.SetMsg(err.Error()))
		return
	}
	if hashPwd != req.Password {
		response.Result(ctx, response.ErrInvalidParam.SetMsg("登录密码与加密密码不匹配"))
		return
	}
	fillAble := model.AccountFillAble{
		Username:  req.Username,
		Password:  req.Password,
		OriginPwd: req.OriginPwd,
		Status:    req.Status,
		CompanyId: req.CompanyId,
	}
	response.Result(ctx, accountSrv.Create(fillAble, util.CurrentLogin(ctx)))
	return
}

// Show 账户详情
func (r AccountHandler) Show(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	err, account := accountSrv.FindByID(uint(id), util.CurrentLogin(ctx))
	if err != nil {
		response.Result(ctx, response.ErrQuery.SetMsg(err.Error()))
		return
	}
	response.Result(ctx, response.OK.WithData(account))
	return
}

// Update 账户更新
func (r *AccountHandler) Update(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	var req request.AccountUpdate
	_ = ctx.ShouldBindJSON(&req)
	if err := verify.AccountUpdate(&req); err != nil {
		response.Result(ctx, response.ErrInvalidParam.SetMsg(err.Error()))
		return
	}
	// 校验原密码与加密密码是否匹配
	hashPwd, err := accountSrv.GetHashPwd(req.OriginPwd)
	if err != nil {
		response.Result(ctx, response.ErrQuery.SetMsg(err.Error()))
		return
	}
	if hashPwd != req.Password {
		response.Result(ctx, response.ErrInvalidParam.SetMsg("登录密码与加密密码不匹配"))
		return
	}
	response.Result(ctx, accountSrv.Update(uint(id), req, util.CurrentLogin(ctx)))
	return
}

// Del 账户删除
func (r AccountHandler) Del(ctx *gin.Context) {
	id, _ := strconv.Atoi(ctx.Param("id"))
	response.Result(ctx, accountSrv.Del(uint(id), util.CurrentLogin(ctx)))
	return
}

// 获取加密密码
func (r AccountHandler) GetHashPwd(ctx *gin.Context) {
	var req request.AccountGetHashPwd
	_ = ctx.ShouldBindJSON(&req)
	if err := verify.AccountGetHashPwd(&req); err != nil {
		response.Result(ctx, response.ErrInvalidParam.SetMsg(err.Error()))
		return
	}
	hashPwd, err := accountSrv.GetHashPwd(req.OriginPwd)
	if err != nil {
		response.Result(ctx, response.ErrQuery.SetMsg(err.Error()))
		return
	}
	response.Result(ctx, response.OK.WithData(hashPwd))
	return
}

// 获取账户信息
func (r AccountHandler) GetAccount(ctx *gin.Context) {
	var req request.AccountGetAccount
	_ = ctx.ShouldBindJSON(&req)
	if err := verify.AccountGetAccount(&req); err != nil {
		response.Result(ctx, response.ErrInvalidParam.SetMsg(err.Error()))
		return
	}
	err, account := accountSrv.GetAccount(req.Username)
	if err != nil {
		response.Result(ctx, response.ErrQuery.SetMsg(err.Error()))
		return
	}
	response.Result(ctx, response.OK.WithData(account))
	return
}
